#include "Camera.hpp"

Camera::Camera()
{
    setPosition( 0.0, 5.0, 0.0 );
    setRotation( 0.0, 0.0, 0.0 );
}

float Camera::x() const
{
    return m_x;
}

float Camera::y() const
{
    return m_y;
}

float Camera::z() const
{
    return m_z;
}

void Camera::setPosition( float newX, float newY, float newZ )
{
    m_x = newX;
    m_y = newY;
    m_z = newZ;
}

void Camera::translate( float dx, float dy, float dz )
{    
    m_x += dx;
    m_y += dy;
    m_z += dz;
}

float Camera::rotX() const
{
    return m_rotX;
}

float Camera::rotY() const
{
    return m_rotY;
}

float Camera::rotZ() const
{
    return m_rotZ;
}

void Camera::setRotation( float newRX, float newRY, float newRZ )
{
    m_rotX = newRX;
    m_rotY = newRY;
    m_rotZ = newRZ;

    while( m_rotX < 0.0 )
        m_rotX += 360.0;
    while( m_rotX > 360.0 )
        m_rotX -= 360.0;

    while( m_rotY < 0.0 )
        m_rotY += 360.0;
    while( m_rotY > 360.0 )
        m_rotY -= 360.0;

    while( m_rotZ < 0.0 )
        m_rotZ += 360.0;
    while( m_rotZ > 360.0 )
        m_rotZ -= 360.0;
}

void Camera::rotate( float drx, float dry, float drz )
{
    m_rotX += drx;
    m_rotY += dry;
    m_rotZ += drz;

    while( m_rotX < 0.0 )
        m_rotX += 360.0;
    while( m_rotX > 360.0 )
        m_rotX -= 360.0;

    while( m_rotY < 0.0 )
        m_rotY += 360.0;
    while( m_rotY > 360.0 )
        m_rotY -= 360.0;

    while( m_rotZ < 0.0 )
        m_rotZ += 360.0;
    while( m_rotZ > 360.0 )
        m_rotZ -= 360.0;
}

void Camera::apply()
{
    glRotatef( m_rotY, 0.0, 1.0, 0.0 );
    glRotatef( m_rotX, 1.0, 0.0, 0.0 );
    glRotatef( m_rotZ, 0.0, 0.0, 1.0 );
    glTranslatef( m_x, -m_y, m_z );

    assert( glGetError() == GL_NO_ERROR );
}
